/*!
 * Currencyr for WordPress - Admin
 * Version 1.0
 *
 * Copyright (c) 2012 Firman Wandayandi
 * Licensed under GNU General Public License Version 2.0 http://www.gnu.org/licenses/gpl-2.0.html
 *
 * @since 1.0
 */

(function ($) {

    $(document).ready(function() {

        $('#currencyr-update').click(function() {
            $btn = $(this);
            $btn.val('Updating...').attr('disabled', 'disabled');
            $.get(ajaxurl, {action: 'currencyr_update'}, function(response) {
                $('#currencyr-updated').text(response);
                $btn.val('Update').removeAttr('disabled');
            }, 'json');
        });

        $('#backlink-add').click(function() {
            $btn = $(this);
            $theID = $(this).siblings(".backlink-id").val();
            $theTitle = $(this).siblings(".backlink-title").val();
            $theLink = $(this).siblings(".backlink-link").val();
            if($theTitle == '' || $theLink == ''){
                return false;
            }
            
            $btn.val('Adding...').attr('disabled', 'disabled');
            $.get(ajaxurl, {action: 'backlink_add', data: {id: $theID, title: $theTitle, link: $theLink}}, function(response) {
                location.reload();
            }, 'json');
        });
        
        $('.backlink-delete').click(function() {
            $btn = $(this);
            $idtodelete = $(this).parents("td").siblings(".backlink-id").text();

            if($idtodelete == ''){
                return false;
            }
            
            $btn.val('Deleting...').attr('disabled', 'disabled');
            $.get(ajaxurl, {action: 'backlink_delete', data: {backlink_id: $idtodelete}}, function(response) {
                location.reload();
            }, 'json');
        });
        
        $('.backlink-edit').click(function() {
            $btn = $(this);
            $idtoedit = $(this).parents("td").siblings(".backlink-id").text();
            $titletoedit = $(this).parents("td").siblings(".backlink-title").text();
            $linktoedit = $(this).parents("td").siblings(".backlink-link").text();
            
            console.log($idtoedit);
            
            $("input.backlink-id").val($idtoedit);
            $("input.backlink-link").val($linktoedit);
            $("input.backlink-title").val($titletoedit);
            
            $("#backlink-add").val("Update Backlink");
            if($(".reset-backlink").length == 0){
                $("#backlink-add").after($("<input class='button reset-backlink' type='button' value='Reset'>"));
            }
        });
        
        $('.reset-backlink').live("click", function(e) {
            console.log("Hello");
            $btn = $(this);
            $(this).siblings(".backlink-id").val("");
            $(this).siblings(".backlink-title").val("");
            $(this).siblings(".backlink-link").val("");
            
            $("#backlink-add").val("Add Backlink");
            $btn.remove();
        });

    });

})(jQuery);
